package com.jty.lib_base.utils

import android.graphics.Bitmap
import com.czl.lib_base.config.AppConstants
import com.orhanobut.logger.Logger
import java.io.File
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.IOException

fun getPicFilePath(userId: Int, dir: String, fileName: String, strNext: String): String? {
    var dirPath: String = AppConstants.Path.FILE_PATH
    dirPath = dirPath + File.separator + userId + File.separator + dir
    val picDirF = File(dirPath)
    if (!picDirF.exists()) {
        val mkdirs = picDirF.mkdirs()
        if (!mkdirs) {
            Logger.e("m1:创建失败picDir：$dirPath")
        } else {
            Logger.i("m1:创建成功picDir")
        }
    }
    val filePath = dirPath + File.separator + fileName + strNext + ".png"
    Logger.i("m1: 图片存储的路径：$filePath")
    return filePath
}

fun getDowApkPath(userId: Int, dir: String, fileName: String): String? {
    var dirPath: String = AppConstants.Path.FILE_PATH
    dirPath = dirPath + File.separator + userId + File.separator + dir
    val picDirF = File(dirPath)
    if (!picDirF.exists()) {
        val mkdirs = picDirF.mkdirs()
        if (!mkdirs) {
            Logger.e("m1:创建失败picDir：$dirPath")
        } else {
            Logger.i("m1:创建成功picDir")
        }
    }
    val filePath = dirPath + File.separator + fileName
    Logger.i("m1: apk下载路径：$filePath")
    return filePath
}

fun getDowApkDir(userId: Int, dir: String): String? {
    var dirPath: String = AppConstants.Path.FILE_PATH
    dirPath = dirPath + File.separator + userId + File.separator + dir
    val picDirF = File(dirPath)
    if (!picDirF.exists()) {
        val mkdirs = picDirF.mkdirs()
        if (!mkdirs) {
            Logger.e("m1:创建失败picDir：$dirPath")
        } else {
            Logger.i("m1:创建成功picDir")
        }
    }
    val filePath = dirPath
    Logger.i("m1: apk下载路径：$filePath")
    return filePath
}

/**
 * 获取图片压缩路径
 */
fun getCompressPath(dir: String): String? {
    var dirPath: String = AppConstants.Path.FILE_PATH
    dirPath = dirPath + File.separator + dir
    val picDirF = File(dirPath)
    if (!picDirF.exists()) {
        val mkdirs = picDirF.mkdirs()
        if (!mkdirs) {
            Logger.e("m1:创建失败picDir：$dirPath")
        } else {
            Logger.i("m1:创建成功picDir")
        }
    }
    return picDirF.path
}

fun getLocalAvatarPath(url:String):String{
    var dir = AppConstants.Path.FILE_PATH + File.separator + "avatar"
    if (!File(dir).exists()){
        File(dir).mkdirs()
    }
    var fileName = url.substring(url.lastIndexOf("/").plus(1))
    return dir + File.separator + fileName
}

/**
 * 保存方法
 */
fun saveBitmap(bitmap: Bitmap, filePath: String) {
    Logger.i("保存图片路径：${filePath}")
    var f = File(filePath)
    if (f.exists()) {
        f.delete()
        //return;
    }
    var type = ""
    type = if ("PNG" == filePath.substring(filePath.lastIndexOf(".") + 1).toUpperCase()) {
        ".png"
    } else {
        ".jpg"
    }
    try {
        val out = FileOutputStream(f)
        var result = false
        result = if (".png" == type) {
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)
        } else {
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)
        }
        out.flush()
        out.close()
        if (!result) {
            Logger.e("保存图片失败，马上会删除这一张图片")
            f = File(filePath)
            if (f.exists()) {
                f.delete()
            }
        }
    } catch (e: FileNotFoundException) {
        e.printStackTrace()
    } catch (e: IOException) {
        e.printStackTrace()
    }
}